import HttpRequest from './axios'
import {Message} from 'view-design'
import store from "../store";


const axios = new HttpRequest()
const ajax = (option) => {
    return new Promise((resolve, reject) => {
        axios.request(option).then(res => {
            if (res.success) {
                resolve(res)
            } else {
                Message.error(res.msg)
                reject(res)
            }
        }).catch(error => {
            reject(error)
        })
    })
}

const get = (url, data) => {
    return ajax({
        url,
        method: 'get',
        data
    })
}

const post = (url, data, msgType = 'Message') => {
    let moduleGroupUrl = localStorage.getItem('moduleGroupUrl') || store.state.app.moduleGroupUrl
    if (moduleGroupUrl){
        return corspost({url:moduleGroupUrl + url,data})
    }else{
        return ajax({
            url,
            method: 'post',
            data,
            msgType
        })
    }

}

const corspost = ({url, data}) => {
    return ajax({
        url: '/mp/cors/request',
        method: 'post',
        data: {url, params: JSON.stringify(data)}
    })
}

export {ajax, get, post, corspost}
